package cn.yj.core.business.service;

import cn.yj.annotation.pagehelper.page.Page;
import cn.yj.cgs.dba.entity.Images;
import cn.yj.cgs.dba.entity.Product;
import cn.yj.cgs.dba.entity.ProductType;
import cn.yj.cgs.dba.entity.vo.ProductVo;
import cn.yj.cgs.dba.mapper.ImagesMapper;
import cn.yj.cgs.dba.mapper.ProductMapper;
import cn.yj.common.Enums;
import cn.yj.common.UUIdUtils;
import cn.yj.commons.utils.FilterNull;
import cn.yj.commons.utils.MapUtils;
import cn.yj.commons.utils.StringUtils;
import cn.yj.params.check.annotation.CheckObjectValue;
import cn.yj.params.check.annotation.KeyValue;
import cn.yj.params.check.annotation.Require;
import cn.yj.tools.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 永健
 * @since 2021-06-15 15:09
 */
@Service
public class ProductService extends ServiceImpl<ProductMapper, Product> {

    @Resource
    private ImagesMapper imagesMapper;

    public Page<ProductVo> pageList(@Require Map<String, Object> params, @Require Page<ProductVo> page) {
        baseMapper.pageList(params, page);
        return page;
    }

    public Page<ProductType> typePageList(@Require Map<String, Object> params, @Require Page<ProductType> page) {
        baseMapper.typePageList(params, page);
        return page;
    }

    public List<ProductType> typeList() {
        return baseMapper.selectListType();
    }

    @CheckObjectValue(keyValue = @KeyValue(type = ProductType.class, name = {"name"}))
    public boolean saveAndUpdateType(ProductType productType) {
        Integer id = productType.getId();
        String name = productType.getName();
        ProductType p = baseMapper.selectByName(name);
        if (StringUtils.isNull(id)) {
            productType.setTypeNo(UUIdUtils.getCode());
            if (StringUtils.isNotNull(p)) {
                throw new ServiceException("该类型已存在");
            }
            return baseMapper.insertType(productType);
        }

        if (StringUtils.isNotNull(p) && !p.getId().equals(id)) {
            throw new ServiceException("该类型已存在");
        }

        return baseMapper.updateTypeById(productType);
    }

    public boolean removeTypeByNo(@Require List<String> typeNo) {
        if (typeNo.isEmpty()) {
            return false;
        }

        if (baseMapper.countByTypeNo(typeNo) > Enums.Common.ZERO) {
            throw new ServiceException("存在和商品绑定的类型");
        }
        typeNo.forEach(no -> baseMapper.deleteTypeByNo(no));
        return true;
    }

    public boolean removeByNo(List<Integer> ids) {
        return baseMapper.deleteBatchIds(ids) > Enums.Common.ZERO;
    }

    public List<Images> selectProductImg(String productNo) {
        return imagesMapper.selectList(new QueryWrapper<Images>().lambda().eq(Images::getType,
                Enums.Images.TYPE.PRODUCT.name()).eq(Images::getCode, productNo));
    }

    /**
     * 更新审核状态
     *
     * @param body
     */
    @CheckObjectValue(keyValue = @KeyValue(type = Map.class, name = {"status", "productNo"}))
    public boolean updateStatus(Map<String, Object> body) {
        Product product = baseMapper.selectByNo(MapUtils.getStringVal(body, "productNo"));
        FilterNull.isNull(product, "物品不存在");
        String status = MapUtils.getStringVal(body, "status");
        Enums.Article.STATUS[] values = Enums.Article.STATUS.values();

        // 状态类型校验
        boolean flag = false;
        for (int i = 0; i < values.length; i++) {
            Enums.Article.STATUS value = values[i];
            if (value.name().equals(status)) {
                flag = true;
                break;
            }
        }
        if (!flag) {
            throw new ServiceException("审核状态异常");
        }
        Product p = new Product();
        p.setProductNo(product.getProductNo());
        p.setAuditStatus(status);
        return super.update(p, new UpdateWrapper<Product>().lambda().eq(Product::getProductNo, product.getProductNo()));
    }
}
